import { reactive } from 'vue'
import { userInfo } from './userInfo'
// 证件类型自定义校验规则
const validateCertificates = (_rule: any, value: any, callback: any) => {
  const household = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  const idCard = /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/

  if (userInfo.certificatesType === '10') {
    if (idCard.test(value)) {
      callback()
    } else {
      callback(new Error('请确认所选证件类型是否与输入证件号码一致  '))
    }
  } else if (userInfo.certificatesType === '20') {
    if (household.test(value)) {
      callback()
    } else {
      callback(new Error('请确认所选证件类型是否与输入证件号码一致'))
    }
  } else {
    callback(new Error('请确认所选证件类型是否与输入证件号码一致'))
  }
}
// 校验规则
export const rules = reactive({
  name: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  certificatesType: [{ required: true, message: '请选择证件类型', trigger: 'blur' }],
  certificatesNo: [
    { required: true, message: '请输入证件号码', trigger: 'blur' },
    { validator: validateCertificates, trigger: 'blur' }, //自定义验证规则
  ],
  certificatesUrl: [{ required: true, message: '请上传证件照片', trigger: 'blur' }],
})
